class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

    def __str__(self):
        return str(self.val)+"->"+str(self.next)

def reverse_ll_rec(head):
    if not head.next:
        return head
    new_head = reverse_ll_rec(head.next)
    head.next.next = head
    head.next = None
    return new_head

def reverse_ll_loop(head):
    pre = None
    cur = head
    while cur:
        next = cur.next
        cur.next, cur, pre = pre, next, cur
    return pre

def to_node(nodes):
    node_list = [Node(x) for x in nodes]
    for i in range(1, len(nodes)):
        node_list[i-1].next = node_list[i]
    return node_list[0]

print(reverse_ll_rec(to_node([1,1,6,3,4,5,6])))
print(reverse_ll_loop(to_node([1,1,6,3,4,5,6])))